<?php
  class sitemap
  {
      private $sitemapHandler;
      private $uri;
      private $uriBase; 
      private $params;
      private $paramCount;
      private $uriArr;
      private $urlCount;
      private $controller;
      private $method;
            
      public function __construct() 
      {
          if(isset($_SERVER['REDIRECT_QUERY_STRING']))
          {
                $uri = $_SERVER['REDIRECT_QUERY_STRING'];    
          }
          else
          {
              $uri = '';
          }
          
          $this -> sitemapHandler = array();
          $this -> uri = $uri;
          $this -> divideUri();
          $this -> siteMapGenerator();
          $this -> getUrl();
      }
      
      public function siteMapGenerator()
      {
          $this -> generateRedirection('auth', 'loginForm', 'zaloguj');
          $this -> generateRedirection('main', 'main_site', '');
          $this -> generateRedirection('auth', 'login', 'logowanie');
          $this -> generateRedirection('auth', 'logout', 'wyloguj');
          $this -> generateRedirection('main', 'pageNotFound', 'nie-ma-takiej-strony');
      }
      
      private function generateRedirection($controller, $method, $url, $paramsCount=0)
      {
          $redirect = array();
          $redirect['controller'] = $controller;
          $redirect['method'] = $method;
          $redirect['url'] = $url;
          $redirect['paramsCount'] = $paramsCount;
          array_push($this -> sitemapHandler,$redirect);   
      }
      
      public function getUrl()
      {
          $checkIfUrl = false;
          foreach($this -> sitemapHandler as $key => $value)
          {
              $base = $this -> uriArr[0];
              $baseCount = 1;
              for($i=1;$i<($this -> urlCount - $value['paramsCount']-1);$i++)
              {
                  $baseCount++;
                  $base .= "/".$this -> uriArr[$i];
              }
               
              if($value['url'] == $base)
              {
                  if($baseCount + $value['paramsCount'] == $this -> urlCount)
                  {
                      $checkIfUrl = true;
                      $this -> controller = $value['controller'];
                      $this -> paramCount = $value['paramsCount'];
                      $this -> method = $value['method'];
                      $this -> uriBase = $value['url'];
                  }
              }
          }
          
          if(!$checkIfUrl)
          {
                $this -> siteNotFound();   
          }
      }
      
      private function siteNotFound()
      {
          $friendlyLink = SITE_ADDRESS."nie-ma-takiej-strony.html";
          header("Location:$friendlyLink");
          die();
      } 
      
      private function divideUri()
      {
          $temp = explode('q=', $this -> uri);
          if(isset($temp[1]))
          {
            $this -> uriArr = explode('/', $temp[1]);    
          }
          else
          {
            $temp[0] = "";
            $this -> uriArr = $temp;    
          }
          
          $this -> urlCount = count($this -> uriArr);
      }
      
      public function getUriParam($number)
      {
          return $this -> uriArr[$number];
      }
      
      public function getUriBase()
      {
          return $this -> uriBase;
      }
      
      public function getUri()
      {
          return $this -> uri;
      }
      
      public function getController()
      {
          return $this -> controller;
      }
      
      public function getMethod()
      {
            return $this -> method;          
      }
  }
?>